/**
 * 
 */
package com.apchernykh.rozhlasapp;

import java.util.List;

import com.apchernykh.ceskyrozhlas.basic.R;
import com.apchernykh.types.RadioProgram;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class RadioProgramAdapter extends ArrayAdapter<RadioProgram> {
	final int mLayoutId;

	public RadioProgramAdapter(Context iContext, int iTextViewResourceId,
			List<RadioProgram> iObjects) {
		super(iContext, iTextViewResourceId, iObjects);
		mLayoutId = iTextViewResourceId;
	}

	private void setViewText(String iText, TextView iView) {
		if (iView != null) {
			if (iText == null || iText.length() == 0) {
				iView.setVisibility(View.GONE);
			} else {
				iView.setVisibility(View.VISIBLE);
				iView.setText(iText);
			}
		}
	}

	public View getView(int iPosition, View iConvertView, ViewGroup iParent) {
		View aCurrentRow = iConvertView;
		if (aCurrentRow == null) {
			LayoutInflater inflater = ((Activity) getContext())
					.getLayoutInflater();
			aCurrentRow = inflater.inflate(mLayoutId, iParent, false);
		}
		if (iPosition < getCount()) {
			RadioProgram aItem = getItem(iPosition);
			// Log.d("RadioProgramAdapter", "showing " + aItem.toString());
			setViewText(aItem.mName, (TextView) aCurrentRow.findViewById(R.id.title));
			setViewText(aItem.mDescription,
					(TextView) aCurrentRow.findViewById(R.id.popis_poradu));
			setViewText(aItem.formatTimeInterval(),
					(TextView) aCurrentRow.findViewById(R.id.time));
			// timeTextView.setText(aItem.mTimeTermin + " " + aItem.mType);
		}

		return aCurrentRow;
	}
}